/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef OTABYTEPACKER_H
#define OTABYTEPACKER_H

/**
	@author Warren Kenny <warren.kenny@gmail.com>
*/

#include <string.h>
#include <string>

class otaBytePacker
{
public:
	otaBytePacker();
		
	template<class T>
	static bool appendBasicType( char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, const T& variable )
	{
		return appendBuffer( data, dataSize, offset, newOffset, &variable, sizeof(variable) );
	}

	template<class T>
	static bool removeBasicType( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, T& variable )
	{
		return removeBuffer( data, dataSize, offset, newOffset, &variable, sizeof(variable) );
	}
	
	static bool removeNTString( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, std::string &string );
	static bool removeLEString( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, std::string &string );
	static bool removeLEBinary( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, unsigned long &value );
	static bool removeStaticString( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, unsigned int stringLength, char string[]);
	
	static bool bufferCopy( char *destination, unsigned int destinationSize, const unsigned int offset, unsigned int &newOffset, const void *source, const unsigned int sourceSize ); 
	
	~otaBytePacker();

protected:
	
	static bool appendBuffer( char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, const void *const writeBuffer, const unsigned int writeSize );
	
	static bool removeBuffer( const char *data, const unsigned int dataSize, const unsigned int offset, unsigned int &newOffset, void *const readBuffer, const unsigned int readSize );
	
};

#endif

